// pages/oneself/redact/redact.js
const myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
const myemail = /^[0-9a-zA-Z]{3,20}\@[0-9a-zA-Z]{1,5}\.[a-z]{1,5}$/;
const format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;
const app = getApp()
const connector = require('../../../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    array: ['请选择学校', 'A1大型客车', 'A2牵引车', 'A3城市公交车', 'B1中型客车', 'B2大型货车', 'C1手动挡', 'C2自动挡', 'D、E摩托车'],
    userList:{
      idcard: "",
      mail_address: "",
      phone: "",
      realname: "",
      // school_id: "暂未填写",
    },
    // sex:'1',
    // types: [//所有班型
    //   { name: 'A1', value: 'A1大型客车'},
    //   { name: 'A2', value: 'A2牵引车' },
    //   { name: 'A3', value: 'A3城市公交车' },
    //   { name: 'B1', value: 'B1中型客车' },
    //   { name: 'B2', value: 'B2大型货车' },
    //   { name: 'C1', value: 'C1手动挡' },
    //   { name: 'C2', value: 'C2自动挡' },
    //   { name: 'D、E', value: 'D、E摩托车' },
      
    // ],
    // typesStr:'请选择班型',
    // typesView:false,//班型选择框弹出/关闭
    // typesVal:[],//班型选择结果
    index:0,
    identity: app.globalData.identity,//身份：false：学员（默认）true：教练
    schoolIndex:0,
    array:[],

  },
  //测试：身份转换
  identity(e) {
    this.setData({
      identity: !app.globalData.identity
    })
    app.globalData.identity = !app.globalData.identity

  },
  //打开/关闭班型选择弹框
  typesView(e){
    this.setData({
      typesView: !this.data.typesView
    })
  },
   //获取班型

  bxxzBc(e){
    let that = this
    // console.log(e.detail.value)
    let arr = e.detail.value.types
    let str = ''
    console.log(arr)
    for(let i=0;i<arr.length;i++){
      if(i == 0){
        str += arr[i]
      }else{
        str = str + "," + arr[i]
      }
    }
    console.log(str)
    that.setData({
      typesVal: arr,
      typesView: false,
      typesStr:str
    })

  },
   // 选择学校
   bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      schoolIndex: e.detail.value
    })
  },
  //表单提交
  formSubmit: function (e) {
    var that = this
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let list = e.detail.value
    // console.log(list.name)
    if (list.realname == '') {
      wx.showToast({
        title: "请输入姓名",
        icon: 'none',
        duration: 1000
      })
      return false
    }

    // if (list.idcard == '') {
    //   wx.showToast({
    //     title: "请输入18位身份证号码",
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return false
    // }else{
    //   if (list.idcard.length < 18 || !format.test(list.idcard)) {
    //     wx.showToast({
    //       title: "身份证号输入有误",
    //       icon: 'none',
    //       duration: 1000
    //     })
    //     return false
    //   }
    // }
    if (list.phone == '') {
      wx.showToast({
        title: "请输入联系电话",
        icon: 'none',
        duration: 1000
      })
      return false
    } else {
      if (list.phone.length < 11 || !myreg.test(list.phone)) {
        wx.showToast({
          title: "联系电话输入有误",
          icon: 'none',
          duration: 1000
        })
        return false
      }
    }
    if (list.mail_address == '') {
      wx.showToast({
        title: "请输入邮寄地址",
        icon: 'none',
        duration: 1000
      })
      return false
    }
    console.log(wx.getStorageSync("userInfo"))

    console.log(wx.getStorageSync("userInfo").address_id)
    if(wx.getStorageSync("userInfo").address_id == 0){
      if (that.data.schoolIndex == 0) {
        wx.showToast({
          title: "请选择学校",
          icon: 'none',
          duration: 1000
        })
        return false
      }
  
      list.school_id = that.data.array[that.data.schoolIndex].school_id
    }else{
      list.school_id = 0
    }
   
    console.log(list)
    vipUserEdit(that,list)
    // wxUpd(that, e.detail.value)

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    getShool(this)
    let that = this
    let userInfo = wx.getStorageSync("userInfo")
    // console.log(userInfo)
    // console.log(user)
    that.setData({
      userInfo:userInfo,
      // tempFilePaths:user.picture
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu()
    let user = wx.getStorageSync("myUser")
    let name = user.wuName,
      phone = user.userPhone,
      // sex = user.sex,
      email = user.email;
    if (name == '' || !name) name = ""

    if (phone == '' || !phone) phone = ""

    // if (sex == 1) sex = "男"
    // else if (sex == 2) sex = "女"
    // else sex = ""

    if (email == '' || !email) email = ""
    this.setData({
      phone: phone,
      name: name,
      // sex: sex,
      email: email
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },

})



//获取所有学校
function getShool(that){
  connector.getFuncSchool(function(res){
    console.log(res)
    if (res.statusCode == 200){
      if(wx.getStorageSync("users")){
        let user = wx.getStorageSync("users")
        console.log(user)
        if((user).constructor === Array){
          
        }else{
          let userList={
            idcard: user.idcard,
            mail_address: user.mail_address,
            phone: user.phone,
            realname: user.realname,
            // school_id: "暂未填写",
          }
          let schoolIndex;
          for(let i=0;i<res.data.length;i++){
            if(res.data[i].school_id == user.school_id){
              schoolIndex = i
            }
          }
          console.log(userList)
          that.setData({
            schoolIndex:schoolIndex,
            userList:userList
          })
        }
        
      }
      
      that.setData({
        array: res.data
      })
    }else{
      wx.showToast({
        title: res.errMsg,
        icon: 'none',
        duration: 1000
      })
    }
  },function(res){
    console.log(res)
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//更新个人信息
function vipUserEdit(that,obj){
  let user= {}
  if(wx.getStorageSync("users")){
    obj.id = wx.getStorageSync("users").id
    user = wx.getStorageSync("users")
  }
  connector.getFunc(connector.connector().vipUserEdit, obj, "post", app.tokens, function (res) {
    console.log(obj)
    if (res.data.result == 1) {
      userIfo(that)
      
      user.idcard = obj.idcard
      user.mail_address = obj.mail_address
      user.phone = obj.phone
      user.realname = obj.realname
      user.school = that.data.array[that.data.schoolIndex]
      console.log(user)
      wx.setStorageSync("users",user)
      wx.showToast({
        title: "保存成功",
        icon: 'none',
        duration: 1000
      })
      wx.navigateBack({
        delta: 1,
      })
    }else{
      wx.showToast({
        title: "保存是失败，请重试",
        icon: 'none',
        duration: 1000
      })
    }
   
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//获取我的信息
function userIfo(that){
  // vip_status是否购买理赔；0：未购买；1：科目一
  connector.getFunc(connector.connector().userIfo, "", "get", app.tokens, function (res) {
    console.log(res)
    if (res.statusCode == 200) {
      let identity = false
      wx.setStorageSync("userInfo",res.data.user_info)
      let myId;
      console.log(res.data.user_info.user_id)
      if(res.data.user_info.coach){
        identity = true
        myId = res.data.user_info.user_id
      }else{
        myId = res.data.user_info.pid
        identity = false
      }
      app.globalData.myId = myId
      app.globalData.identity = identity
      app.globalData.isYes = true
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}